import { Global, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { MongooseModule } from '@nestjs/mongoose';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Resource, ResourceSchema } from './models/resource';
import { User, UserSchema } from './models/user';
import { RoleModule } from './modules/role/role.module';
import { UserModule } from './modules/user/user.module';
import { ResourceModule } from './modules/resource/resource.module';

export const models = MongooseModule.forFeature([
  { name: Resource.name, schema: ResourceSchema },
  { name: User.name, schema: UserSchema },
]);

@Global()
@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    MongooseModule.forRoot('mongodb://localhost:27017/nest-auth', {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      useFindAndModify: false,
    }),
    models,
    RoleModule,
    UserModule,
    ResourceModule,
  ],
  controllers: [AppController],
  providers: [AppService],
  exports: [models],
})
export class AppModule {}
